<?php
namespace Tiger\Api\Utils;

use App\Transformers\ApiError;
use Endroid\QrCode\QrCode;
use Illuminate\Support\Facades\Log;

class Helper
{



    static public function  QrCode($info,$logo=null,$size=300)
    {

        $qrCode = new QrCode();

        $filename = md5(microtime()) . '.' . strtolower($qrCode->getImageType());
       // $realPrefix='../storage';
        $storageRelativeDir = '/app/code/'.date('Ymd').'/';


        if (!is_dir(storage_path($storageRelativeDir))) {
            mkdir(storage_path() . $storageRelativeDir, 0777, true);
        }
        chmod(storage_path() .$storageRelativeDir, 0777);
        if(!is_writable(storage_path().$storageRelativeDir)) {
            ApiError::errorForbidden('文件夹没有写权限');
        }

        $realFileName=storage_path().$storageRelativeDir.$filename;


        if($logo)
        {
            //$qrCode->setText(json_encode($info))
            $qrCode->setText($info)
                ->setSize($size)
                ->setPadding(10)
                ->setLogo($logo)
                ->setErrorCorrection('high')
                ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
                ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
                ->setLabelFontSize(16)
                ->setImageType(QrCode::IMAGE_TYPE_PNG)
                ->save($realFileName);
        }
        else
        {
            $qrCode->setText($info)
                ->setSize($size)
                ->setPadding(10)
                ->setErrorCorrection('high')
                ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
                ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
                ->setLabelFontSize(16)
                ->setImageType(QrCode::IMAGE_TYPE_PNG)
                ->save($realFileName);
        }
       return  $url=url('storage'.$storageRelativeDir.$filename);
    }


    static function do_Post($url, $fields, $extraheader = array(),$json=false){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($fields)) {
            if($json && is_array($fields)){
             $fields = json_encode( $fields );
             }

            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            if($json){
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($fields)));
            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

   static function do_Get($url, $extraheader = array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $extraheader);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回:
        //curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch) ;

        curl_close($ch);
        return $output;
    }




static function  call_http_get($url,$data,$header=[])
    {
        $param=http_build_query($data);
        $buf = Helper::do_Get($url.'?'.$param,$header);
        return json_decode($buf, True);
    }


    static  function call_http_post($url,$data,$header=[], $json = false)
    {
        $buf =Helper::do_POST($url,$data,$header,$json);
        if($json)
            return $buf;
        return json_decode($buf, True);
    }






}